package xuegao.study.mall.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.model.ums.UmsMember;
import xuegao.study.mall.web.feign.UmsMemberReceiveAddressClient;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.ums.UmsMemberReceiveAddress;
import xuegao.study.mall.web.service.UmsMemberService;

import java.util.List;

@RestController
@RequestMapping("/member/address")
public class UmsMemberReceiveAddressController {

    @Autowired
    private UmsMemberService umsMemberService;

    @Autowired
    private UmsMemberReceiveAddressClient umsMemberReceiveAddressClient;

    @PostMapping("/add")
    public CommonResult add(@RequestBody UmsMemberReceiveAddress address) {
        UmsMember currentMember = umsMemberService.getCurrentMember();
        address.setMemberId(currentMember.getId());
        return umsMemberReceiveAddressClient.add(address);
    }

    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        return umsMemberReceiveAddressClient.delete(id);
    }

    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody UmsMemberReceiveAddress address) {
        return umsMemberReceiveAddressClient.update(id, address);
    }

    @GetMapping("/list")
    public CommonResult<List<UmsMemberReceiveAddress>> list() {
        UmsMember currentMember = umsMemberService.getCurrentMember();
        Long memberId = currentMember.getId();
        return umsMemberReceiveAddressClient.list(memberId);
    }

    @GetMapping("/{id}")
    public CommonResult<UmsMemberReceiveAddress> getItem(@PathVariable Long id) {
        return umsMemberReceiveAddressClient.getItem(id);
    }
}
